package generator

import (
	"CommonsProject/models/generatorModel"
	"bytes"
	_ "embed"
	"errors"
	"html/template"
)

//go:embed template/models.templ
var models string

//go:embed template/controller.templ
var controller string

//go:embed template/service.templ
var service string

//go:embed template/repository.templ
var repository string

//go:embed template/routers.templ
var routers string

//go:embed template/sql.templ
var sql string

func Models(table generatorModel.Table) string {
	return tool(models, table)
}
func Controller(table generatorModel.Table) string {
	return tool(controller, table)
}
func Service(table generatorModel.Table) string {
	return tool(service, table)
}

func Repository(table generatorModel.Table) string {
	return tool(repository, table)

}
func Routers(table generatorModel.Table) string {
	return tool(routers, table)
}
func SQL(table generatorModel.Table) string {
	return tool(sql, table)
}

func tool(templ string, value any) string {
	parseFS, err := template.New("service").Parse(templ)
	if err != nil {
		panic(errors.New("解析模板失败"))
	}
	var buf bytes.Buffer
	err = parseFS.Execute(&buf, value)
	if err != nil {
		panic(errors.New("解析模板失败"))
	}
	return buf.String()
}
